Skip to content

perf: reuse single tsx register for TS imports#248

Open
Karavil wants to merge 1 commit intorocicorp:mainfrom
goblinshq:optimize-tsx-register
Open

perf: reuse single tsx register for TS imports#248
Karavil wants to merge 1 commit intorocicorp:mainfrom
goblinshq:optimize-tsx-register

Conversation

@Karavil
Copy link

@Karavil Karavil commented Feb 26, 2026

Summary

  • tsImport() from tsx/esm/api creates a fresh module.register() + MessageChannel per call. The CLI invokes it up to 3 times (once in config.ts, twice in drizzle-kit.ts).
  • This PR adds a shared register({namespace}) singleton that lazily initializes on first use and reuses its .import() method for all subsequent TS file imports.
  • Micro-benchmark: 2x tsImport = ~96ms vs shared register + 2x .import() = ~3ms (~90ms saved per extra call).

Changes

  • New src/cli/ts-import.ts: lazy singleton wrapper around tsx register().
  • src/cli/config.ts: replace tsImport() with tsImportShared().
  • src/cli/drizzle-kit.ts: replace both tsImport() calls with tsImportShared().

Test plan

  • npx vitest run passes (same pre-existing failures as main)
  • Generated output is identical to baseline (diff clean)
  • Micro-benchmark confirms ~30x faster for repeated imports

@0xcadams 0xcadams marked this pull request as ready for review February 26, 2026 22:06
tsImport() from tsx/esm/api creates a new module.register() and
MessageChannel per invocation. The CLI calls it up to 3 times. By
creating a single register({namespace}) instance and reusing its
.import() method, we eliminate redundant hook registrations.

Micro-benchmark shows ~90ms savings per additional tsImport call
(96ms for 2x tsImport vs 3ms for shared register + 2x import).

* Add src/cli/ts-import.ts with lazy shared register singleton
* Replace tsImport() in config.ts (1 call) and drizzle-kit.ts (2 calls)
@Karavil Karavil force-pushed the optimize-tsx-register branch from 7447593 to a51fd3f Compare February 26, 2026 22:07
@0xcadams 0xcadams self-requested a review February 26, 2026 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant